package com.nblog.controller.admin;
import com.jfinal.aop.Before;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.nblog.annotation.ControllerBind;
import com.nblog.auth.annotation.RequiresPermissions;
import com.nblog.interceptor.admin.LoginInterceptor;
import com.nblog.interceptor.admin.SysLogInterceptor;
import com.nblog.model.SysRole;
import com.nblog.plugin.ioc.annotation.JFAutowired;
import com.nblog.service.SysRoleService;
import com.nblog.utils.CommonUtils;
import com.nblog.view.InvokeResult;
@ControllerBind(controllerKey="/admin/role")
@Before({LoginInterceptor.class,SysLogInterceptor.class})
public class RoleController extends BaseControlller {
	@JFAutowired
	SysRoleService sysRoleService;
	@RequiresPermissions(value={"/admin/role"})
	public void index(){
		render("index.html");
	}
	@RequiresPermissions(value={"/admin/role"})
	public void getData(){
		String name=this.getPara("name");
		int page=getParaToInt("page", 1);
		int rows=getParaToInt("rows", 20);
		Page<SysRole> pageInfo=sysRoleService.getPage(page, rows,name,this.getOrderbyStr());
		renderJson(CommonUtils.pageToDataGrid(pageInfo));
	}
	@RequiresPermissions(value={"/admin/role"})
	public void getZtree(){
		Integer type=this.getParaToInt("type");
		Integer roleId=this.getParaToInt("roleId");
		InvokeResult result=sysRoleService.getZtreeViewList(type, roleId);
		System.out.println(JsonKit.toJson(result));
		this.setAttr("jsonTree", result);
		this.setAttr("roleId", roleId);
		render("menu_assign.html");
	}
	
	/**
	 * 角色绑定资源.
	 */
	
	@Before(Tx.class)
	@RequiresPermissions(value={"/admin/role"})
	public void saveMenuAssign(){
		Integer roleId=this.getParaToInt("roleId");
		String menuIds=this.getPara("menuIds");
		InvokeResult result=sysRoleService.saveMenuAssign(menuIds, roleId);
		this.renderJson(result);
	}
	@RequiresPermissions(value={"/admin/role"})
	public void edit(){
		render("edit.html");
	}
	@RequiresPermissions(value={"/admin/role"})
	public void saveRole(){
		Integer id=getParaToInt("id");
		String name=getPara("name");
		String des=getPara("des");
		renderJson(sysRoleService.save(id, name, des));
	}
	@RequiresPermissions(value={"/admin/role"})
	public void setVisible(){
		Integer visible=this.getParaToInt("visible");
		String bids=this.getPara("ids");
		InvokeResult result=sysRoleService.setVisible(bids, visible);
		this.renderJson(result);
	}
	@RequiresPermissions(value={"/admin/role"})
	@Before({POST.class,Tx.class})
	public void del(){
		String idStrs=this.getPara("ids");
		this.renderJson(sysRoleService.del(idStrs));
	}
}
